package xyf.p02.stack_queue;

import java.util.Stack;

/**
 * ClassName: P1047_RemoveDuplicates
 * Description:
 * Author: xyf
 * Date: 9/3/22 6:33 PM
 * Version: 1.0
 **/
public class P1047_RemoveDuplicates
{
    public String removeDuplicates(String s)
    {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++)
        {
            if (stack.isEmpty())
            {
                stack.push(s.charAt(i));
                continue;
            }

            if (stack.peek().equals(s.charAt(i)))
            {
                stack.pop();
            }
            else
            {
                stack.push(s.charAt(i));
            }
        }

        if (stack.isEmpty())
        {
            return "";
        }

        StringBuilder res = new StringBuilder();
        while (!stack.isEmpty())
        {
            res.append(stack.pop());
        }
        return res.reverse().toString();
    }
}
